﻿#if NETSTANDARD2_0 || NETCOREAPP3_0
using Longbow.Data;
using System;

namespace Microsoft.Extensions.DependencyInjection
{
    /// <summary>
    /// 注入数据库服务扩展类
    /// </summary>
    public static class DatabaseServiceCollectionExtensions
    {
        /// <summary>
        /// 获取 容器集合
        /// </summary>
        internal static ServiceProvider ServiceProvider { get { return _lazy.Value; } }

        private static Lazy<ServiceProvider> _lazy;
        /// <summary>
        /// 增加数据库适配框架到服务容器中 <see cref="IServiceCollection" />.
        /// </summary>
        /// <param name="services">The <see cref="IServiceCollection" /> to add services to.</param>
        /// <returns>The <see cref="IServiceCollection"/> so that additional calls can be chained.</returns>
        public static IServiceCollection AddDbAdapter(this IServiceCollection services)
        {
            // 创建服务
            services.AddSingleton(typeof(IDbContext<>), typeof(DefaultDbContext<>));
            services.AddSingleton(typeof(IDbAdapter), typeof(DefaultDbAdapter));
            _lazy = new Lazy<ServiceProvider>(() => services.BuildServiceProvider());
            return services;
        }
    }
}
#endif
